/*----------------------------------------------------------------------------
 * os_obj.h: CMSIS-RTOS global object definitions for an application
 *----------------------------------------------------------------------------
 *
 * This header file defines global RTOS objects used throughout a project
 *
 * #define osObjectsPublic indicates that objects are defined; without that
 * definition the objects are defined as external symbols.
 *
 *--------------------------------------------------------------------------*/

#ifndef __os_obj__
#define __os_obj__

#ifdef __os_obj_creat__
#define OBJ_EXT
#else
#define OBJ_EXT extern
#endif

#include <rtthread.h>
#include <rtconfig.h>
#include <cmsis_rt.h>

/*****************************************************************************
* @brief   tick
*****************************************************************************/
#define ms_ticks(x) (((x) * RT_TICK_PER_SECOND + 999) / 1000)

/*****************************************************************************
* @brief   prio
*****************************************************************************/
#define PRIO_BELOW_LOW (RT_THREAD_PRIORITY_MAX - 5)
#define PRIO_LOW (PRIO_BELOW_LOW - 1)
#define PRIO_ABOVE_LOW (PRIO_LOW - 1)
#define PRIO_BELOW_MIDDLE (PRIO_ABOVE_LOW - 1)
#define PRIO_MIDDLE (PRIO_BELOW_MIDDLE - 1)
#define PRIO_ABOVE_MIDDLE (PRIO_MIDDLE - 1)
#define PRIO_BELOW_HIGH (PRIO_ABOVE_MIDDLE - 1)
#define PRIO_HIGH (PRIO_BELOW_HIGH - 1)
#define PRIO_ABOVE_HIGH (PRIO_HIGH - 1)
#define PRIO_REAL (2)

#if (PRIO_ABOVE_HIGH <= 2) || (PRIO_ABOVE_HIGH >= RT_THREAD_PRIORITY_MAX) || (PRIO_BELOW_LOW >= RT_THREAD_PRIORITY_MAX)
#error TASK_PRIOTITY ERROR @ osOObjCreat.h file ...
#endif

/*****************************************************************************
* @brief   stack
*****************************************************************************/
#define STK_SIZE(x) (x)

/*****************************************************************************
* @brief   rtos object
*****************************************************************************/
typedef struct
{
/*****************************************************************************
* @brief   thread
*****************************************************************************/
#define SYS_TASK_STK_SIZE STK_SIZE(0x1000)
#define SYS_TASK_PRIO PRIO_REAL
	struct rt_thread tcb_systask;
	rt_thread_t tid_systask;
	rt_uint64_t stk_systask[SYS_TASK_STK_SIZE / 8];

#define TASK_MODBUS_STK_SIZE STK_SIZE(0x400)
#define TASK_MODBUS_PRIO PRIO_BELOW_HIGH
	struct rt_thread tcb_modbus;
	rt_thread_t tid_modbus;
	rt_uint64_t stk_modbus[TASK_MODBUS_STK_SIZE / 8];

#define TASK_COM_CMD_STK_SIZE STK_SIZE(0x800)
#define TASK_COM_CMD_PRIO PRIO_BELOW_MIDDLE
	struct rt_thread tcb_console;
	rt_thread_t tid_console;
	rt_uint64_t stk_console[TASK_COM_CMD_STK_SIZE / 8];

#define TASK_GUI_STK_SIZE STK_SIZE(0x800)
#define TASK_GUI_PRIO PRIO_LOW
	struct rt_thread tcb_gui;
	rt_thread_t tid_gui;
	rt_uint64_t stk_gui[TASK_COM_CMD_STK_SIZE / 8];

/*****************************************************************************
* @brief   OS object
*****************************************************************************/
#define OS_EVENT_DEFINE

	struct rt_event ecb_modbus;
	rt_event_t eid_modbus;

	// malloc mutex
	struct rt_mutex mcb_malloc;
	rt_mutex_t mid_malloc;

} os_obj_type;

OBJ_EXT os_obj_type os_obj;

int os_obj_create(void);
int os_obj_init(void);

void os_lock(void);
void os_unlock(void);

void os_task_info(void);

#endif // __os_obj__
